package com.mirror.controller;

import com.mirror.entity.building.Building;
import com.mirror.entity.building.Floor;
import com.mirror.entity.oldman.Result;
import com.mirror.repository.BuildingRepository;
import com.mirror.repository.FloorRepository;
import com.mirror.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(description = "楼层管理")
@RestController
@RequestMapping("floor")
public class FloorController {


    @Autowired
    private BuildingRepository buildingRepository;
    @Autowired
    private FloorRepository floorRepository;


    @ApiOperation(value = "楼层列表无分页", notes = "楼层列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"buildingId\": \"建筑id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/list")
    public Result list(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String buildingId = tempData.getString("buildingId");
        Building b = buildingRepository.findOne(buildingId);
        if (b == null) {
            return ResultUtil.error(-1, "获取建筑失败");
        }
        List<Floor> blist = floorRepository.findAllByIsdelAndBuildingOrderByFloorName(false, b);
        return ResultUtil.success(blist);
    }

    @ApiOperation(value = "楼层列表", notes = "楼层列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"page\": \"页码\",\"size\": \"每页数量\",\"buildingId\": \"建筑id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/pagelist")
    public Result atList(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        int page = tempData.getInt("page");
        int size = tempData.getInt("size");
        String buildingId = tempData.getString("buildingId");
        Building b = buildingRepository.findOne(buildingId);
        if (b == null) {
            return ResultUtil.error(-1, "获取建筑失败");
        }
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Floor> atPage = floorRepository.findAllByIsdelAndBuildingOrderByFloorName(pageable, false, b);
        return ResultUtil.success(atPage);
    }


    @ApiOperation(value = "添加楼层", notes = "添加楼层")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"buildingId\": \"建筑id\",\"floorName\": \"楼层名\",\"memo\": \"备注\"}", required = true, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String buildingId = tempData.getString("buildingId");
        String memo = tempData.optString("memo");
        String floorName = tempData.getString("floorName");
        Floor a = new Floor();
        a.setCreateDate(new Date());
        Building b = buildingRepository.findOne(buildingId);
        if (b == null) {
            return ResultUtil.error(-1, "获取建筑失败！");
        }
        a.setBuilding(b);
        a.setMemo(memo);
        a.setFloorName(floorName);
        a.setRoomsNum(0);
        a.setIsdel(false);
        b.setFloorsNum(b.getFloorsNum() + 1);
        buildingRepository.save(b);
        return ResultUtil.success(floorRepository.save(a));
    }

    @ApiOperation(value = "修改楼层", notes = "修改楼层")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"buildingId\": \"建筑id\",\"id\": \"楼层id\",\"floorName\": \"楼层名\",\"memo\": \"备注\"}", required = true, dataType = "String"),
    })
    @PostMapping("/edit")
    public Result edit(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.getString("id");
        String buildingId = tempData.getString("buildingId");
        String memo = tempData.optString("memo");
        String floorName = tempData.getString("floorName");
        Floor a = floorRepository.findOne(id);
        if (a == null) {
            return ResultUtil.error(-1, "获取楼层失败！");
        }
        Building b = buildingRepository.findOne(buildingId);
        if (b == null) {
            return ResultUtil.error(-1, "获取建筑失败！");
        }
        a.setMemo(memo);
        a.setFloorName(floorName);
        a.setBuilding(b);
        return ResultUtil.success(floorRepository.save(a));
    }

    @ApiOperation(value = "删除楼层", notes = "删除楼层")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "{\"id\": \"楼层id\"}", required = true, dataType = "String"),
    })
    @PostMapping("/del")
    public Result del(@RequestBody String data) {
        JSONObject tempData = new JSONObject(data);
        String id = tempData.getString("id");
        Floor a = floorRepository.findOne(id);
        if (a == null) {
            return ResultUtil.error(-1, "获取楼层失败！");
        }
        a.setIsdel(true);
        a.getBuilding().setFloorsNum(a.getBuilding().getFloorsNum() - 1);
        buildingRepository.save(a.getBuilding());
        return ResultUtil.success(floorRepository.save(a));
    }

}

